package com.youlai.boot.bmp.controller;

import com.youlai.boot.bmp.service.ProcessCategoryService;
import com.youlai.boot.core.web.PageResult;
import com.youlai.boot.core.web.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.bmp.model.form.ProcessCategoryForm;
import com.youlai.boot.bmp.model.query.ProcessCategoryQuery;
import com.youlai.boot.bmp.model.vo.ProcessCategoryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 流程分类前端控制层
 *
 * @author rabit
 * @since 2025-10-22 21:32
 */
@Tag(name = "21.流程分类接口")
@RestController
@RequestMapping("/api/v1/process-category")
@RequiredArgsConstructor
public class ProcessCategoryController  {

    private final ProcessCategoryService processCategoryService;

    @Operation(summary = "流程分类分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('bmp:process:category:query')")
    public PageResult<ProcessCategoryVO> getProcessCategoryPage(ProcessCategoryQuery queryParams ) {
        IPage<ProcessCategoryVO> result = processCategoryService.getProcessCategoryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增流程分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('bmp:process:category:add')")
    public Result<Void> saveProcessCategory(@RequestBody @Valid ProcessCategoryForm formData ) {
        boolean result = processCategoryService.saveProcessCategory(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取流程分类表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('bmp:process:category:edit')")
    public Result<ProcessCategoryForm> getProcessCategoryForm(
        @Parameter(description = "流程分类ID") @PathVariable Long id
    ) {
        ProcessCategoryForm formData = processCategoryService.getProcessCategoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改流程分类")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('bmp:process:category:edit')")
    public Result<Void> updateProcessCategory(
            @Parameter(description = "流程分类ID") @PathVariable Long id,
            @RequestBody @Validated ProcessCategoryForm formData
    ) {
        boolean result = processCategoryService.updateProcessCategory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除流程分类")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('bmp:process:category:delete')")
    public Result<Void> deleteProcessCategories(
        @Parameter(description = "流程分类ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = processCategoryService.deleteProcessCategorys(ids);
        return Result.judge(result);
    }
}
